<template>
  <div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <FilterTable ref="table" :columns="columns" :dataSource="tableList" :isSelectColumn="false" height="auto" :onColumnsChange="columns => (this.columns = columns)">
      <template slot="controls" slot-scope="props"> </template>
    </FilterTable>
    <BreakSpace label="操作记录" />
    <FilterTable ref="tables" :columns="columns1" :dataSource="tableLists" :datakey="'records'" :isSelectColumn="false" height="auto" :onColumnsChange="columns => (this.columns1 = columns)">
    </FilterTable>

    <BaseDialog :visible.sync="isDiscount" :title="'折扣详情'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <DiscountDetail :rowData="DiscountData" @close="Discountclose" />
    </BaseDialog>
    <div style="height:47px;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
    </div>
  </div>
</template>
<script>
import { getLog } from '@/api/dealerSale/OrderManagement/OrderSubmission/index.js';
import { getAmount, getDiscount, getDiscountDetails, saveOrder, getNotPage, getDealerAddress, getDetail } from '@/api/dealerSale/OrderManagement/OrderDetailQuery/index.js';
import { dictionary } from '@/utils/dictMixin';
import Cookies from 'js-cookie';
import DiscountDetail from './DiscountDetail';
import moment from 'moment';
export default {
  data() {
    this.saveData = null;
    this.Amount = null;
    this.singleRebateTotalAmount = null;
    this.buildRebateTotalAmount = null;
    return {
      formList: this.createdFrom(),
      columns: this.createdTable(),
      tableList: [],
      isaddCar: false,
      carData: {},
      json: {},
      isDiscount: false,
      DiscountData: {},
      TotalAmount: '',
      singleRebateAmount: '',
      buildRebateAmount: '',
      dealerCode: Cookies.get('ownerCode'),
      columns1: this.createdTables(),
      tableLists: []
    };
  },
  components: {
    DiscountDetail
  },
  props: ['rowData'],
  created() {
    this.getDetail(this.rowData);
    this.getAmountDetail(this.dealerCode);
    this.getLog(this.rowData.orderId);
    this.getDiscountDetails(this.rowData);
  },
  mounted() {
    this.table = this.$refs.table.$refs.pageTable;
  },
  mixins: [dictionary],
  methods: {
    createdFrom() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '订单信息'
        },

        {
          type: 'INPUT',
          label: '订货单编号',
          fieldName: 'orderNo',
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'DATE',
          label: '当前年月',
          fieldName: 'YearMonth',
          placeholder: '请输入',
          disabled: true,
          dateType: 'month',
          initialValue: moment().format('YYYY-MM')
        },
        {
          type: 'SELECT',
          label: '订单状态',
          fieldName: 'orderStatus',
          placeholder: '请选择',
          disabled: true,
          itemList: this.createDictList('6002')
        },
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          disabled: true,
          itemList: this.createDictList('6006')
        },
        {
          type: 'SELECT',
          label: '订单细分类型',
          fieldName: 'orderDetailType',
          placeholder: '请选择',
          disabled: true,
          itemList: []
        },
        {
          type: 'INPUT',
          label: '订单经销商',
          fieldName: 'dealerName',
          placeholder: '请选择',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '产品代码',
          fieldName: 'productCode',
          placeholder: '请选择',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '产品名称',
          fieldName: 'productName',
          placeholder: '请选择',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '内饰',
          fieldName: 'trimName',
          placeholder: '请选择',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '销售订单号',
          fieldName: 'doNo',
          placeholder: '请选择',
          disabled: true
        },
        {
          type: 'INPUT',
          label: 'VIN码',
          fieldName: 'vin',
          placeholder: '请选择',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '外饰',
          fieldName: 'colorName',
          placeholder: '请选择',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '收车地址',
          fieldName: 'address',
          placeholder: '请选择',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '资金类型',
          fieldName: 'accountTypeName',
          placeholder: '请选择',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '运输方式',
          fieldName: 'transportType',
          disabled: true,
          itemList: this.createDictList('6004')
        },
        // 2021-6-29 14:16:54 为避免无谓的解释，宏远说都去掉；
        // {
        //   type: 'INPUT',
        //   label: '资金总余额',
        //   fieldName: 'totalAmount',
        //   placeholder: '请选择',
        //   disabled:true,
        // },
        // {
        //   type: 'INPUT',
        //   label: '单车返利余额',
        //   fieldName: 'singleRebateAmounts',
        //   placeholder: '请选择',
        //   disabled:true,
        // },
        // {
        //   type: 'INPUT',
        //   label: '建店返利余额',
        //   fieldName: 'buildRebateAmounts',
        //   placeholder: '请选择',
        //   disabled:true,
        // },
        {
          type: 'INPUT',
          label: '订单总金额',
          fieldName: 'singleAmount',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '是否使用返利',
          fieldName: 'isRebate',
          disabled: true,
          itemList: this.createDictList('1004')
        },
        {
          type: 'INPUT',
          label: '返利使用金额',
          fieldName: 'rebateAmount',
          disabled: true
        },
        {
          type: 'DATE',
          label: '要求交付日期',
          fieldName: 'requiredDeliveryDate',
          disabled: true,
          dateType: 'exactdate'
        },
        {
          type: 'INPUT',
          label: '单车返利金额',
          fieldName: 'singleRebateAmount',
          disabled: true
        },
        // {
        //   type: 'INPUT',
        //   label: '建店返利金额',
        //   fieldName: 'buildRebateAmount',
        //   disabled: true
        // },
        {
          type: 'INPUT',
          label: '建店补贴直接带出池',
          fieldName: 'buildRebateAmountStart',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '建店补贴单车带出池',
          fieldName: 'buildRebateAmount',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '折扣率(%)',
          fieldName: 'discount',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '市场指导价',
          fieldName: 'singleAmount',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '折扣总金额',
          fieldName: 'discountAmount',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '折后单价',
          fieldName: 'afterDiscountAmount',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'remark',
          disabled: true,
          selfCols: 2
        },
        {
          type: 'INPUT',
          label: '实付总金额',
          fieldName: 'afterDiscountRebateAmount',
          disabled: true
        },
        {
          type: 'BREAK_SPACE',
          label: '价格折扣信息'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '折扣规则名称',
          dataIndex: 'ruleName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '动力形式',
          dataIndex: 'powerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6008'),
          render: p => {
            return <span>{this.createDictText(p.row.powerType, '6008')}</span>;
          }
        },
        {
          title: '折扣种类',
          dataIndex: 'discountType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6032'),
          render: p => {
            return <span>{this.createDictText(p.row.discountType, '6032')}</span>;
          }
        },
        {
          title: '折扣计算方式',
          dataIndex: 'discountMethod',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6033'),
          render: p => {
            return <span>{this.createDictText(p.row.discountMethod, '6033')}</span>;
          }
        },
        {
          title: '折扣值',
          dataIndex: 'discountValue',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (p, h) => {
            return <span>{p.row.discountMethod == 60331001 ? p.row.discountValue : p.row.discountValue + '%'}</span>;
          }
        },
        {
          title: '单车折扣金额',
          dataIndex: 'discountAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '折扣前单价',
          dataIndex: 'singleAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '折扣后单价',
          dataIndex: 'afterDiscountAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    createdTables() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '单据执行前状态',
          dataIndex: 'beforeOrderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6043'),
          render: p => {
            return <span>{this.createDictText(p.row.beforeOrderStatus, '6043')}</span>;
          }
        },
        {
          title: '单独执行后状态',
          dataIndex: 'afterOrderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6043'),
          render: p => {
            return <span>{this.createDictText(p.row.afterOrderStatus, '6043')}</span>;
          }
        },
        {
          title: '操作人员',
          dataIndex: 'createdName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '操作时间',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '操作类型',
          dataIndex: 'operationType',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: p => {
            return <span>{p.row.operationType}</span>;
          }
        },
        {
          title: '审批意见',
          dataIndex: 'auditOpinion',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    changeHandle(val) {
      this.saveData = val;
    },
    async getAmountDetail(code) {
      let res = await getAmount({ dealerCode: code });
      let total = 0,
        buildRebateAmount = 0,
        singleRebateAmount = 0;
      total = Number(res.data.totalEnergyAmount) + Number(res.data.totalFuelAmount);
      buildRebateAmount = Number(res.data.buildRebateEnergyAmount) + Number(res.data.buildRebateFuelAmount);
      singleRebateAmount = Number(res.data.singleRebateEnergyAmount) + Number(res.data.singleRebateFuelAmount);
      for (let i in res.data) {
        this.formList.map(x => {
          if (x.fieldName == 'totalAmount') {
            x.initialValue = total;
          } else if (x.fieldName == 'singleRebateAmounts') {
            x.initialValue = singleRebateAmount;
          } else if (x.fieldName == 'buildRebateAmounts') {
            x.initialValue = buildRebateAmount;
          } else if (x.fieldName == i) {
            x.initialValue = res.data[i];
          }
        });
      }
    },
    async closeDrawer(type) {
      if (type == 1) {
        this.$emit('close');
      }
    },
    Discountclose() {
      this.isDiscount = false;
    },
    // 加载折扣信息
    async getNotPage(orderType) {
      let list = await getNotPage({ orderType: orderType });
      this.formList.map(x => {
        if (x.fieldName == 'orderDetailType') {
          x.itemList = list.data.map(x => ({ text: x.subName, value: x.subId }));
        }
      });
    },
    editHandle(data, type) {
      this.DiscountData = data;
      this.isDiscount = true;
    },
    // 获取详情
    async getDetail(data) {
      let res = await getDetail({ id: data.id });
      let num = 0;

      for (let i in res.data) {
        this.formList.map(x => {
          if (x.fieldName == 'totalAmount') {
          } else if (i == x.fieldName) {
            x.initialValue = res.data[i];
          }
        });
      }
      this.getNotPage(res.data.orderType);
    },
    async getDiscountDetails(data) {
      let json = { policyId: data.policyId, singleAmount: data.singleAmount, subsidyAmount: data.subsidyAmount };
      let res = await getDiscountDetails(json);
      this.tableList = res.data || [];
    },
    async getLog(id) {
      let res = await getLog({ orderId: id });
      console.log(res, '2');
      this.tableLists = res.data || [];
    }
  }
};
</script>
<style lang="less">
.discount {
  text-decoration: underline;
  color: #d1291a;
  cursor: pointer;
}
</style>
